package com.hangkong.controller;

import com.hangkong.model.Flight;
import com.hangkong.service.FlightService;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
@RequestMapping("/flight")
public class FlightController {


    private Logger log = Logger.getLogger(UserController.class);

    @Resource
    private FlightService flightService;

    @RequestMapping("/showFlights")
    @ResponseBody
    public List<Flight> showFlights(){
            List<Flight> flightList=flightService.getAll();
            return flightList;
    }

    @RequestMapping("/showone")
    @ResponseBody
    public Object showone(Integer id){
        Flight obj=flightService.showone(id);
        return obj;
    }

    @RequestMapping("/update")
    @ResponseBody
    public String update(HttpServletRequest request){
        String id=request.getParameter("flightid");
        String flightno=request.getParameter("flightno");
        String deptcity=request.getParameter("deptcity");
        String arrcity=request.getParameter("arrcity");
        System.out.println(Integer.parseInt(id));
        flightService.update(Integer.parseInt(id),flightno,deptcity,arrcity);
        return "hey";
    }

    @RequestMapping("/insertFlight")
    @ResponseBody
    public String insertFlight(HttpServletRequest request){
        String deptcity=request.getParameter("deptcity");
        String flightno=request.getParameter("flightno");
        String arrcity=request.getParameter("arrcity");
        String flightdate=request.getParameter("flightdate");

        String cabinnamef=request.getParameter("cabinnamef");
        String cabinpricef=request.getParameter("cabinpricef");
        String countf=request.getParameter("countf");

        String cabinnamey=request.getParameter("cabinnamey");
        String cabinpricey=request.getParameter("cabinpricey");
        String county=request.getParameter("county");

        String cabinnamee=request.getParameter("cabinnamee");
        String cabinpricee=request.getParameter("cabinpricee");
        String counte=request.getParameter("counte");

        String cabinnamer=request.getParameter("cabinnamer");
        String cabinpricer=request.getParameter("cabinpricer");
        String countr=request.getParameter("countr");


        flightService.insert(flightno,deptcity,arrcity,flightdate);
        Integer maxid = flightService.findmaxid();
        if (countf!=null){
            flightService.insertcabF(maxid,cabinnamef,cabinpricef,countf);
        }
        if (county!=null){
            flightService.insertcabY(maxid,cabinnamey,cabinpricey,county);
        }
        if (counte!=null){
            flightService.insertcabE(maxid,cabinnamee,cabinpricee,counte);
        }
        if (countr!=null){
            flightService.insertcabR(maxid,cabinnamer,cabinpricer,countr);
        }
        return "123";
    }

    @RequestMapping("/delFlight")
    @ResponseBody

    public Object delFlight(@RequestParam(value = "ids[]")String[] ids){

        System.out.println(ids);

           for (String id:ids) {
               System.out.println(id);
               flightService.del(Integer.parseInt(id));
           }
        return "123";
    }

    @RequestMapping("/findone")
    @ResponseBody
    public Object findone(HttpServletRequest request){
            String deptcity=request.getParameter("deptc");
            String arrcity=request.getParameter("arrc");
            String flightdate=request.getParameter("flightd");
            Flight obj=flightService.findone(deptcity,arrcity,flightdate);
        System.out.println(obj);
            return obj;
    }


}
